/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.common.other;

public class SmartTokenizer {

    private String[] parts;

    public SmartTokenizer(String textArg, String token, String stringLimiter, boolean coalesceDuplicateTokens) {

        if (!Utils.isBlankOrNull(stringLimiter)) {
            checkStringLimiterCount(textArg, stringLimiter);
        }

        // Rebuild the text without duplicate tokens if required.
        String text;
        if (coalesceDuplicateTokens) {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            boolean withinStringLimiters = false;
            while (i <= textArg.length() - 1) {
                String here = textArg.substring(i, i + 1);
                if (here.equals(stringLimiter)) {
                    withinStringLimiters = !withinStringLimiters;
                }
                boolean skipIt = false;
                if (!withinStringLimiters && here.equals(token)) {
                    if (i > 0) {
                        // Was the previous one a token too? If so, skip this one.
                        String there = textArg.substring(i - 1, i);
                        if (there.equals(token)) {
                            skipIt = true;
                        }
                    }
                }
                if (!skipIt) {
                    sb.append(here);
                }
                i++;
            }
            text = sb.toString().trim();

        } else {
            text = textArg.trim();
        }

        int i = 0;
        int tokenCount = 1;
        boolean withinStringLimiters = false;
        while (i <= text.length() - 1) {
            String here = text.substring(i, i + 1);
            if (here.equals(stringLimiter)) {
                withinStringLimiters = !withinStringLimiters;
            }
            if (!withinStringLimiters && here.equals(token)) {
                tokenCount++;
            }
            i++;
        }
        parts = new String[tokenCount];

        i = 0;
        withinStringLimiters = false;
        int part = 0;
        int start = 0;
        while (i <= text.length() - 1) {
            String here = text.substring(i, i + 1);
            if (here.equals(stringLimiter)) {
                withinStringLimiters = !withinStringLimiters;
            }
            if (!withinStringLimiters && here.equals(token)) {
                String x = text.substring(start, i).trim();
                if (!Utils.isBlankOrNull(stringLimiter) && x.startsWith(stringLimiter) && x.endsWith(stringLimiter)) {
                    x = x.substring(1, x.length() - 1);
                }
                parts[part++] = x;
                start = i + 1;
            }
            i++;
        }
        if (start <= text.length()) {
            String x = text.substring(start, text.length()).trim();
            if (!Utils.isBlankOrNull(stringLimiter) && x.startsWith(stringLimiter) && x.endsWith(stringLimiter)) {
                x = x.substring(1, x.length() - 1);
            }
            parts[part] = x;
        }
    }

    private static void checkStringLimiterCount(String textArg, String stringLimiter) {
        boolean odd = false;
        for (int i = 0; i < textArg.length(); i++) {
            if (textArg.charAt(i) == stringLimiter.charAt(0)) {
                odd = !odd;
            }
        }
        if (odd) {
            throw new IllegalStateException("Odd number of string limiters in " + textArg);
        }
    }

    public String[] getParts() {
        return parts;
    }
}